<template>
  <div class="mt-1">
    <van-form>
      <h4 style="margin: 0 0 0.2rem 1rem; color: gray">
        当前产区:{{ this.$store.getters.productionLine }}
      </h4>
      <van-field
        v-model="code"
        clearable
        ref="pmfocus"
        @focus="stopKeyborad"
        label="拍码"
        placeholder="请输入拍码"
      />
      <ag-grid-vue
        style="width: 100%; height: 62vh"
        class="ag-theme-alpine mt-1"
        :columnDefs="columnDefs"
        :defaultColDef="defaultColDef"
        :gridOptions="gridOptions"
        :singleClickEdit="true"
        rowSelection="multiple"
        :rowData="rowData"
      >
      </ag-grid-vue>
      <van-popup
        v-model="ShowStatue"
        position="bottom"
        style="height: 100%"
        class="bg-default"
      >
        <order-data
          :ShowDataA="ShowData"
          @get-val="SureBottom"
          v-if="ShowStatue"
        />
      </van-popup>
      <audio src="../../assets/warning.mp3" preload="auto" id="bgMusic"></audio>
    </van-form>
  </div>
</template>

<script>
import { AgGridVue } from "ag-grid-vue";
import { CODE_LENGTH } from "../../config";
import { AgGrid } from "../../mixins/AgGrid";
import { parseQRCode, toast } from "../../utils/common";
import { commissionSubmit, SerachList } from "../../api/feeding"; //
import OrderData from "../../components/OrderData.vue";

export default {
  name: "WIP",
  components: { AgGridVue, OrderData },
  mixins: [AgGrid],
  data() {
    return {
      code: "",
      savenum: "",
      ShowStatue: false,
      columnDefs: [
        { headerName: "物料号", field: "ProductId", width: 90 },
        { headerName: "物料名称", field: "ProductName", width: 125 },
        { headerName: "生产日期", field: "ProductionDate", width: 100 },
        { headerName: "数量", field: "Num", width: 70 },
      ],
      rowData: [],
      ShowData: {},
    };
  },
  watch: {
    code(val) {
      if (val.length > CODE_LENGTH) {
        let res = parseQRCode(val);
        if (res) {
          this.findPat(res.palletNo, res.materialCode);
        } else {
          this.playMusic();
          toast("无法识别");
        }
      }
      this.code = "";
      this.$refs.pmfocus.focus();
    },
  },
  methods: {
    // 消除获取焦点时自动弹出键盘
    stopKeyborad() {
      this.$refs.pmfocus.$refs.input.setAttribute("readonly", "readonly");
      setTimeout(() => {
        this.$refs.pmfocus.$refs.input.removeAttribute("readonly");
      }, 200);
    },
    //播放警示音
    playMusic() {
      setTimeout(() => {
        let audio = document.getElementById("bgMusic");
        audio.play();
      }, 200);
    },
    async SureBottom(val, palletNo, materialCode) {
      this.ShowStatue = false;
      let user = this.$store.getters.user;
      let productionline = this.$store.getters.productionLine;
      let res = await commissionSubmit(
        productionline,
        palletNo,
        materialCode,
        "10",
        user,
        "2",
        "",
        this.$store.getters.WarehouseId,
        "",
        "",
        val
      );
      console.log(res);
      if (res && res.Status == 200 && res.Data) {
        this.rowData.push({
          ProductName: this.ShowData.ProductName,
          ProductId: this.ShowData.ProductId,
          UniqueId: this.ShowData.UniqueId,
          Num: val,
          ProductionDate: this.ShowData.ProductionDate.substring(0, 10),
          OutStorageBillDetailId: this.ShowData.OutStorageBillDetailId,
        });
        toast(res.Msg);
      } else {
        this.playMusic();
        toast(res.Msg);
      }
      this.$refs.pmfocus.focus();
    },
    //从外界添加数据
    async findPat(palletNo) {
      /*  if (!this.rowData.find((it) => it.UniqueId === palletNo)) { */
      let productionline = this.$store.getters.productionLine;
      //let user = this.$store.getters.user;
      let res = await SerachList(
        productionline,
        palletNo,
        this.$store.getters.WarehouseId
      );
      if (res.Status == 200 && res.Data != null) {
        this.ShowStatue = true;
        this.ShowData = JSON.parse(JSON.stringify(res.Data));
      } else {
        if (res.Status == 200 && res.Data == null) {
          this.playMusic();
          toast("此物料查询不到");
        } else {
          this.playMusic();
          toast(res.Msg);
        }
      }
      console.log(res);
      this.$refs.pmfocus.focus();
      /* let res = await commissionSubmit(
          productionline,
          palletNo,
          materialCode,
          "7",
          user,
          "2",
          "",
          this.$store.getters.WarehouseId
        );
        console.log(res);
        if (res && res.Status == 200 && res.Data) {
          this.ShowStatue = true;
          this.ShowData = JSON.parse(JSON.stringify(res.Data));
            this.rowData.push({
            ProductName: res.Data.ProductName,
            ProductId: res.Data.ProductId,
            UniqueId: res.Data.UniqueId,
            Num: res.Data.Num,
            ProductionDate: res.Data.ProductionDate.substring(0, 10),
            OutStorageBillDetailId: res.Data.OutStorageBillDetailId,
          }); 
        } else {
          toast(res.Msg);
        } */
      /*  } else {
        this.playMusic();
        toast("此拍已被录入");
      } */
    },
  },
  mounted() {
    this.$refs.pmfocus.focus();
  },
};
</script>
